// admin/system/service/sys_role_menu_service.go
package service

import (
	repo "go-admin1/admin/system/dao"
	"go-admin1/admin/system/model"
)

// SysRoleMenuService 角色菜单关联服务接口
type SysRoleMenuService interface {
	Create(roleMenu *model.SysRoleMenu) error
	GetByRoleID(roleID int64) ([]*model.SysRoleMenu, error)
	GetByMenuID(menuID int64) ([]*model.SysRoleMenu, error)
	DeleteByRoleID(roleID int64) error
	DeleteByMenuID(menuID int64) error
	DeleteByRoleIDAndMenuID(roleID, menuID int64) error
	ListMenuIDsByRoleID(roleID int64) ([]int64, error)
	ListRoleIDsByMenuID(menuID int64) ([]int64, error)
}

type sysRoleMenuService struct {
	roleMenuDao repo.SysRoleMenuDao
}

func (s sysRoleMenuService) Create(roleMenu *model.SysRoleMenu) error {
	return s.roleMenuDao.Create(roleMenu)
}

func (s sysRoleMenuService) GetByRoleID(roleID int64) ([]*model.SysRoleMenu, error) {
	roleMenus, err := s.roleMenuDao.GetByRoleID(roleID)
	if err != nil {
		return nil, err
	}
	return roleMenus, nil
}

func (s sysRoleMenuService) GetByMenuID(menuID int64) ([]*model.SysRoleMenu, error) {
	roleMenus, err := s.roleMenuDao.GetByMenuID(menuID)
	if err != nil {
		return nil, err
	}
	return roleMenus, nil
}

func (s sysRoleMenuService) DeleteByRoleID(roleID int64) error {
	return s.roleMenuDao.DeleteByRoleID(roleID)
}

func (s sysRoleMenuService) DeleteByMenuID(menuID int64) error {
	return s.roleMenuDao.DeleteByMenuID(menuID)
}

func (s sysRoleMenuService) DeleteByRoleIDAndMenuID(roleID, menuID int64) error {
	return s.roleMenuDao.DeleteByRoleIDAndMenuID(roleID, menuID)
}

func (s sysRoleMenuService) ListMenuIDsByRoleID(roleID int64) ([]int64, error) {
	menuIDs, err := s.roleMenuDao.ListMenuIDsByRoleID(roleID)
	if err != nil {
		return nil, err
	}
	return menuIDs, nil
}

func (s sysRoleMenuService) ListRoleIDsByMenuID(menuID int64) ([]int64, error) {
	roleIDs, err := s.roleMenuDao.ListRoleIDsByMenuID(menuID)
	if err != nil {
		return nil, err
	}
	return roleIDs, nil
}

// NewSysRoleMenuService 创建角色菜单关联服务实例
func NewSysRoleMenuService() SysRoleMenuService {
	return &sysRoleMenuService{
		roleMenuDao: repo.NewSysRoleMenuDao(),
	}
}
